﻿using System;
using System.Collections.Generic;
using System.DirectoryServices;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace HostAssistantCore
{
    public class ADUser
    {
        /// <summary>
        /// 添加系统(域)用户
        /// </summary>
        /// <param name="strUserName"></param>
        /// <param name="strPassword"></param>
        /// <param name="strDescription"></param>
        /// <param name="strGroup"></param>
        /// <returns></returns>
        public static bool AddUser(string strUserName, string strPassword, string strDescription, string strGroup)
        {
            using (DirectoryEntry AD = new DirectoryEntry(@"WinNT://" + Environment.MachineName + ",computer"))
            {
                try
                {
                    DirectoryEntry User = null;
                    try
                    {
                        User = AD.Children.Find(strUserName, "user");//找到要删除的用户
                        if (User == null)
                            User = AD.Children.Add(strUserName, "user");
                    }
                    catch
                    {
                        User = AD.Children.Add(strUserName, "user");
                    }

                    User.Invoke("SetPassword", new object[] { strPassword });   // 密码
                    User.Properties["description"].Add(strDescription);  //添加描述
                    //User.Invoke("Put", new object[] { "Description", strDescription });
                    User.Invoke("Put", "UserFlags", 66049); //密码永不过期
                    User.CommitChanges();

                    try
                    {
                        DirectoryEntry Group = AD.Children.Find(strGroup, "group");
                        if (strGroup != null)
                            Group.Invoke("Add", new object[] { User.Path.ToString() });
                    }
                    catch { }
                }
                catch (Exception ex)
                {
                   NLogger.Error(ex.ToString());
                }
            }
            return true;
        }

        /// <summary>
        /// 添加系统(域)用户
        /// </summary>
        /// <param name="strUserName">用户名</param>
        /// <param name="strPassword">密码</param>
        /// <param name="strDescription">描述</param>
        /// <returns></returns>
        public static bool AddUser(string strUserName, string strPassword, string strDescription)
        {

            using (DirectoryEntry AD = new DirectoryEntry(@"WinNT://" + Environment.MachineName + ",computer"))
            {
                try
                {
                    DirectoryEntry User = null;
                    try
                    {
                        User = AD.Children.Find(strUserName, "user");//找到要删除的用户
                        if (User == null)
                            User = AD.Children.Add(strUserName, "user");
                    }
                    catch
                    {
                        User = AD.Children.Add(strUserName, "user");
                    }

                    User.Invoke("SetPassword", new Object[] { strPassword });  //设置密码
                    User.Properties["description"].Add(strDescription);  //添加描述
                    User.CommitChanges();
                    User.Dispose();
                }
                catch (Exception ex)
                {
                   NLogger.Error(ex.ToString());
                }
            }
            return true;
        }



        /*
         * 修改密码
         */
        public static bool PwdModify(string strUserName, string strPassword)
        {
            using (DirectoryEntry AD = new DirectoryEntry(@"WinNT://" + Environment.MachineName + ",computer"))
            {
                try
                {
                    DirectoryEntry User = AD.Children.Find(strUserName);
                    //修改密码
                    User.Invoke("SetPassword", new Object[] { strPassword });
                    User.CommitChanges();
                }
                catch
                {
                    return false;
                }
            }
            return true;
        }

        /// <summary>
        /// 删除系统(域)用户
        /// </summary>
        /// <param name="strUserName">用户名</param>
        /// <returns></returns>
        public static bool RemoveUser(string strUserName)
        {
            try
            {
                using (DirectoryEntry AD = new DirectoryEntry(@"WinNT://" + Environment.MachineName + ",computer"))
                {
                    DirectoryEntry User = AD.Children.Find(strUserName, "user");//找到要删除的用户
                    if (User != null)
                        AD.Children.Remove(User);
                }
            }
            catch
            {
                return false;
            }
            return true;
        }

        /// <summary>
        /// 判断Windows用户是否存在
        /// </summary>
        /// <param name="strUserName"></param>
        /// <returns></returns>
        public static bool ExistUser(string strUserName)
        {
            try
            {
                using (DirectoryEntry AD = new DirectoryEntry(@"WinNT://" + Environment.MachineName + ",computer"))
                {
                    var user = AD.Children.Find(strUserName, "user");
                    return user != null;
                }
            }
            catch
            {
                return false;
            }
        }

        /// <summary>
        /// 添加系统(域)组
        /// </summary>
        /// <param name="strGroupName">组名</param>
        /// <param name="strDescription">描述</param>
        /// <returns></returns>
        public static bool AddGroup(string strGroupName, string strDescription)
        {
            try
            {
                using (DirectoryEntry AD = new DirectoryEntry(@"WinNT://" + Environment.MachineName + ",computer"))
                {
                    DirectoryEntry Group;
                    Group = AD.Children.Add(strGroupName, "group");
                    Group.Properties["description"].Add(strDescription);
                    Group.CommitChanges();
                }
                return true;
            }
            catch
            {
                return false;
            }
        }

        /// <summary>
        /// 修改系统(域)组
        /// </summary>
        /// <param name="strGroupName">组名</param>
        /// <param name="strDescription">描述</param>
        /// <returns></returns>
        public static bool ModifyGroup(string strGroupName, string strDescription)
        {
            try
            {
                using (DirectoryEntry AD = new DirectoryEntry(@"WinNT://" + Environment.MachineName + ",computer"))
                {
                    DirectoryEntry Group = AD.Children.Find(strGroupName);
                    if (Group.Name != null)
                    {
                        Group.Properties["description"].Value = strDescription;
                        Group.CommitChanges();
                    }
                }
                return true;
            }
            catch
            {
                return false;
            }
        }

        /// <summary>
        /// 删除系统(域)组
        /// </summary>
        /// <param name="strGroupName">组名</param>
        /// <returns></returns>
        public static bool RemoveGroup(string strGroupName)
        {
            try
            {
                using (DirectoryEntry AD = new DirectoryEntry(@"WinNT://" + Environment.MachineName + ",computer"))
                {
                    DirectoryEntry Group = AD.Children.Find(strGroupName, "group");
                    if (Group.Name != null)
                    {
                        AD.Children.Remove(Group);
                    }
                }
                return true;
            }
            catch
            {
                return false;
            }
        }

        /// <summary>
        /// 添加组用户
        /// </summary>
        /// <param name="strGroupName">组名</param>
        /// <param name="strUserName">用户名</param>
        /// <returns></returns>
        public static bool AddGroupUser(string strGroupName, string strUserName)
        {
            try
            {
                using (DirectoryEntry AD = new DirectoryEntry(@"WinNT://" + Environment.MachineName + ",computer"))
                {
                    DirectoryEntry Group = AD.Children.Find(strGroupName, "group");// 找到组
                    DirectoryEntry User = AD.Children.Find(strUserName, "user");//找到用户
                    if (Group.Name != null && User.Name != null)
                        Group.Invoke("Add", new Object[] { User.Path });
                }
                return true;
            }
            catch
            {
                return false;
            }

        }

        /// <summary>
        /// 移除组用户
        /// </summary>
        /// <param name="strGroupName">组名</param>
        /// <param name="strUserName">用户名</param>
        /// <returns></returns>
        public static bool RemoveGroupUser(string strGroupName, string strUserName)
        {
            try
            {
                using (DirectoryEntry AD = new DirectoryEntry(@"WinNT://" + Environment.MachineName + ",computer"))
                {
                    DirectoryEntry Group = AD.Children.Find(strGroupName, "group");// 找到组
                    object members = Group.Invoke("Members", null);
                    foreach (object member in (System.Collections.IEnumerable)members)
                    {
                        //获取该组的每个成员
                        DirectoryEntry x = new DirectoryEntry(member);
                        if (strUserName == x.Name) //要移除的用户存在的话，则从该组中移除。
                        {
                            DirectoryEntry User = AD.Children.Find(strUserName, "user");//找到该用户
                            Group.Invoke("Remove", new Object[] { User.Path });
                        }
                    }
                }
                return true;
            }
            catch
            {
                return false;
            }

        }
        /// <summary>
        /// 判断组下的用户是否存在
        /// </summary>
        /// <param name="strGroupName">组名</param>
        /// <param name="strUserName">用户名</param>
        /// <returns></returns>
        public static bool GroupSiteExists(string strGroupName, string strUserName)
        {
            try
            {
                using (DirectoryEntry AD = new DirectoryEntry(@"WinNT://" + Environment.MachineName + ",computer"))
                {
                    DirectoryEntry Group = AD.Children.Find(strGroupName, "group");// 找到组
                    object members = Group.Invoke("Members", null);
                    foreach (object member in (System.Collections.IEnumerable)members)
                    {
                        //获取该组的每个成员
                        DirectoryEntry x = new DirectoryEntry(member);
                        if (strUserName == x.Name) //要移除的用户存在的话，则从该组中移除。
                        {
                            return true;
                        }
                    }
                }
                return false;
            }
            catch
            {
                return false;
            }

        }
    }
}
